package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.PrescriptionMapper;
import com.link_with_health.model.PrescriptionEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.PrescriptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("prescriptionService")
public class PrescriptionServiceImpl extends BaseService implements PrescriptionService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    PrescriptionMapper prescriptionMapper;

    /**
     * 通过处方id获取处方信息
     * @param id 处方id
     * @return  处方信息
     * @throws BaseException 异常
     */
    @Override
    public PrescriptionEntity getPrescriptionById(Long id) throws BaseException {
        PrescriptionEntity entity = null;
        try {
            entity = prescriptionMapper.queryPrescriptionById(id);
        } catch (Exception e){
            logger.error("[query prescription failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("PRESCRIPTION.ID: "+id);

        return entity;
    }

    /**
     * 通过会诊信息获取处方信息
     * @param id 会诊信息
     * @return  处方信息
     * @throws BaseException 异常
     */
    @Override
    public List<PrescriptionEntity> getPrescriptionByInquiry(Long id) throws BaseException {
        List<PrescriptionEntity> prescriptionList = null;
        try {
            prescriptionList = prescriptionMapper.queryPrescriptionByInquiry(id);
        } catch (Exception e) {
            logger.error("[query prescription failed]",e);
        }
        if (prescriptionList == null || prescriptionList.isEmpty())
            throw new NullQueryResultException("PRESCRIPTION.INQUIRY_ID: "+id);
        return prescriptionList;
    }

    /**
     * 通过患者信息获取会诊信息
     * @param id 患者id
     * @return  处方信息
     * @throws BaseException 异常
     */
    @Override
    public List<PrescriptionEntity> getPrescriptionByPatient(Long id) throws BaseException {
        List<PrescriptionEntity> prescriptionList = null;
        try {
            prescriptionList = prescriptionMapper.queryPrescriptionByPatient(id);
        } catch (Exception e) {
            logger.error("[query prescription failed]",e);
        }
        if (prescriptionList == null || prescriptionList.isEmpty())
            throw new NullQueryResultException("PRESCRIPTION INQUIRY.PATIENT_ID: "+id);
        return prescriptionList;
    }

    /**
     * 通过医生id获取处方信息
     * @param id 医生id
     * @return  处方信息
     * @throws BaseException 异常
     */
    @Override
    public List<PrescriptionEntity> getPrescriptionByDoctor(Long id) throws BaseException {
        List<PrescriptionEntity> prescriptionList = null;
        try {
            prescriptionList = prescriptionMapper.queryPrescriptionByDoctor(id);
        } catch (Exception e) {
            logger.error("[query prescription failed]",e);
        }
        if (prescriptionList == null || prescriptionList.isEmpty())
            throw new NullQueryResultException("PRESCRIPTION INQUIRY.DR_ID: "+id);
        return prescriptionList;
    }

    /**
     * 获取所有处方信息
     * @return 处方信息
     * @throws BaseException 异常
     */
    @Override
    public List<PrescriptionEntity> getAllPrescription() throws BaseException {
        List<PrescriptionEntity> prescriptionList = null;
        try {
            prescriptionList = prescriptionMapper.queryAllPrescription();
        } catch (Exception e) {
            logger.error("[query prescription failed]",e);
        }
        if (prescriptionList == null || prescriptionList.isEmpty())
            throw new NullQueryResultException("ALL PRESCRIPTION");

        return prescriptionList;
    }

    /**
     * 增加处方信息
     * @param entity 处方信息
     * @return 改动信息
     * @throws BaseException 异常
     */
    @Override
    public int addPrescription(PrescriptionEntity entity) throws BaseException {
        int result = 0;
        try {
            result = prescriptionMapper.insertPrescription(entity);
        } catch (Exception e) {
            logger.error("[add prescription failed]", e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT PRESCRIPTION: "+entity);

        return result;
    }

    /**
     * 修改处方信息
     * @param entity 处方信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changePrescription(PrescriptionEntity entity) throws BaseException {
        int result = 0;
        try {
            result = prescriptionMapper.updatePrescription(entity);
        } catch (Exception e) {
            logger.error("[change prescription failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("UPDATE PRESCRIPTION: "+entity);
        return result;
    }

    /**
     * 删除处方信息
     * @param id 处方id
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deletePrescription(Long id) throws BaseException {
        int result = 0;
        try {
            result = prescriptionMapper.deletePrescription(id);
        } catch(Exception e){
            logger.error("[delete prescription failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE PRESCRIPTION.ID: "+id);
        return result;
    }
}
